import 'package:bruno/bruno.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import '../AppConfig/Config.dart';
import 'AddGoodsFirst.dart';
import 'LoadingAddressView.dart';
import 'PerfectGoods.dart';

class TMSAddressSelection extends StatefulWidget {
  const TMSAddressSelection({super.key, required this.addressType, required this.addressList});
  final int addressType;
  final List addressList;
  @override
  State<TMSAddressSelection> createState() => _TMSAddressSelectionState();
}

class _TMSAddressSelectionState extends State<TMSAddressSelection> {
  List cityData = [];
  List startAddressOne=[];
  List startCitycodeOne =[];
  Map startCitylocation ={};
  String startCitynameOne ="";
  String detailAddressOne="";
  Map detailMap={};
  String chooseTime="";
  String startTimeDay = "";
  String  startTimeHour ="";
  String  startTimeCode ="";
  String contextPerson="";
  String phoneNum="";
  String unitName="";
  late TextEditingController _companyText = TextEditingController(text:unitName);
  late TextEditingController _contextText = TextEditingController(text:contextPerson );
  late TextEditingController _phoneNumText = TextEditingController(text:phoneNum );
  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    if(widget.addressList.isNotEmpty) {
      unitName=widget.addressList[0];
      startCitynameOne = widget.addressList[10];
      startCitycodeOne=widget.addressList[2];
      detailAddressOne=widget.addressList[3];
      contextPerson=widget.addressList[4];
      phoneNum=widget.addressList[5];
      startTimeDay=widget.addressList[6];
      startTimeCode=widget.addressList[7];
      startTimeHour=widget.addressList[8];
    }
  }
  @override
  Widget build(BuildContext context) {
    final screenSize = MediaQuery.of(context).size;
    return Scaffold(
      backgroundColor: MyColors.backgreyColor,
      appBar: AppBar(
        leading: IconButton(icon: Icon(Icons.arrow_back_ios,color: Colors.black,),onPressed: (){Navigator.pop(context);},),
        centerTitle: true,
        backgroundColor: Colors.white,
        title: Text(widget.addressType==1? "装货地址":"卸货地址",style: TextStyle(color: Colors.black,fontSize: 16.sp,fontWeight: FontWeight.w300),),
      ),
      body: Column(
        children: [
          Expanded(child: ListView(children: [
        //     GestureDetector(
        //       child:   Container(
        //     padding: EdgeInsets.only(top: 10.h,bottom: 10.h,left: 10.w,right: 10.w),
        //   margin: EdgeInsets.only(left: 10.w,right: 10.w,top: 10.h),
        //   decoration: BoxDecoration(
        //       color: Colors.white,
        //       borderRadius: BorderRadius.all(Radius.circular(10))),
        //   child: Row(
        // mainAxisAlignment: MainAxisAlignment.spaceBetween,
        // children: [
        //   Row(
        //     children: [
        //       Text("*",style: TextStyle(color: MyColors.themeColor,fontSize: 14.sp),),
        //       Text(widget.addressType==1?"装货单位":"卸货单位",style: TextStyle(color: Colors.black,fontSize: 14.sp),),
        //     ],
        //           ),
        //         Row(
        //           children: [
        //             Text("请输入装货单位",style: TextStyle(color: MyColors.hintColor,fontSize: 14.sp),),
        //             Icon(Icons.arrow_forward_ios,size: 20,color: MyColors.authTipColor,),
        //           ],
        //         )
        //         ],
        //       ),
        //     ),
        //       onTap: () async {
        //
        //       },
        //
        //     ),
            Container(
              padding: EdgeInsets.only(top: 5.h,bottom: 5.h,left: 10.w,right: 10.w),
              margin: EdgeInsets.only(left: 10.w,right: 10.w,top: 10.h),
              decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.all(Radius.circular(10))),
              child:  Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Row(
                    children: [
                      Text("*",style: TextStyle(color:MyColors.themeColor,fontSize: 14.sp),),
                      Text(widget.addressType==1?"装货单位":"卸货单位",style: TextStyle(color: Colors.black,fontSize: 14.sp),),
                    ],
                  ),
                  Container(
                    width: screenSize.width-150.w,
                    height: 35.h,
                    child:  TextField(
                      controller: _companyText,
                      decoration: InputDecoration(
                        hintText: '请输入单位名称',
                        hintStyle:TextStyle(fontSize: 14.sp,color: MyColors.hintColor),
                        filled: true,
                        fillColor: Colors.white,
                        border: InputBorder.none,
                        alignLabelWithHint: true,
                      ),
                      textAlign: TextAlign.right,
                      style: TextStyle(color: Colors.black),
                      onChanged: (value) {
                        setState(() {
                          unitName=value;
                        });
                      },
                    ),
                  ),
                ],
              ),
            ),
            Container(
              padding: EdgeInsets.only(top: 10.h,bottom: 10.h,left: 10.w,right: 10.w),
              margin: EdgeInsets.only(left: 10.w,right: 10.w,top: 10.h),
              decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.all(Radius.circular(10))),
              child: Column(
                children: [
                  Row(
                    children: [
                      if(widget.addressType==1)
                      Image.asset("lib/Images/OrderList/装货点icon.png",width: 20.w,height: 20.h,),
                      if(widget.addressType==2)
                        Image.asset("lib/Images/OrderList/卸货点icon.png",width: 20.w,height: 20.h,),
                      SizedBox(width: 10.w,),
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        GestureDetector(
                            child:
                            Container(
                                width: screenSize.width/1.5,
                                child:
                              Text(startCitynameOne!=""?startCitynameOne: "必填，请选择省市区",style: TextStyle(color:startCitynameOne!=""?Colors.black: MyColors.hintColor,fontSize: 14.sp),),
                              ),
                            onTap:()async{
                              final result = await  showModalBottomSheet(context: context,
                                  isScrollControlled:true,
                                  isDismissible: false, // 设置为 false，点击外部区域不关闭
                                  enableDrag: false, // 设置为 false，禁用上滑手势关闭
                                  builder: (BuildContext context)
                                  {
                                    return chooseCitySheet(chooseType: 1,cityData: cityData,chooseCityList: startAddressOne,chooseCodeList: startCitycodeOne,);
                                  });
                              if (result != null) {
                                setState(() {
                                  startCitynameOne= result[0]+" "+result[1]+" "+result[2];
                                  startCitycodeOne =[result[3],result[4],result[5]];
                                  startAddressOne=[result[0],result[1],result[2]];
                                });
                              }
                            }
                        ),
                        Divider(height: 16.h,color: MyColors.backgreyColor,),
                        GestureDetector(
                            child:
                            detailAddressOne==""?

                              Text("必填，请输入详细地址",style: TextStyle(color: MyColors.hintColor,fontSize: 14.sp),)
                        : Container(
                              width: screenSize.width/1.5,
                              child:Text(detailAddressOne,style: TextStyle(color: Colors.black,fontSize: 14.sp),),
                             ),
                          onTap: ()async{
                            final result=await showModalBottomSheet(context: context, builder: (BuildContext context) {
                              return checkDetialAddressSheet(region:startAddressOne.isNotEmpty? startAddressOne[1]:"",
                              );});
                            if (result !=null){
                              setState(() {
                                detailMap=result;
                                detailAddressOne=detailMap['address'];
                                startCitynameOne=detailMap['province']+" "+detailMap['city']+" "+detailMap['district'];
                                startCitycodeOne=[detailMap['adcode'].substring(0, 2)+"0000",detailMap['adcode'].substring(0, 4)+"00",detailMap['adcode']];
                                startAddressOne=[result['province'],result['city'],result['district']];
                                startCitylocation=result['location'];
                              });
                            }
                          },
                        ),
                      ],
                    ),
                    ],
                  ),

                ],
              ),
            ),
            Container(
              padding: EdgeInsets.only(top: 5.h,bottom: 5.h,left: 10.w,right: 10.w),
              margin: EdgeInsets.only(left: 10.w,right: 10.w,top: 10.h),
              decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.all(Radius.circular(10))),
              child:  Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                      Text("联系人",style: TextStyle(color: Colors.black,fontSize: 14.sp),),
                  Container(
                    width: screenSize.width-150.w,
                    height: 35.h,
                    child:  TextField(
                      controller: _contextText,
                      decoration: InputDecoration(
                        hintText: '请输入联系人',
                        hintStyle:TextStyle(fontSize: 14.sp,color: MyColors.hintColor),
                        filled: true,
                        fillColor: Colors.white,
                        border: InputBorder.none,
                        alignLabelWithHint: true,
                      ),
                      textAlign: TextAlign.right,
                      style: TextStyle(color: Colors.black),
                      onChanged: (value) {
                        setState(() {
                          contextPerson=value;
                        });
                      },
                    ),
                  ),
                ],
              ),
            ),
            Container(
              padding: EdgeInsets.only(top: 5.h,bottom: 5.h,left: 10.w,right: 10.w),
              margin: EdgeInsets.only(left: 10.w,right: 10.w,top: 10.h),
              decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.all(Radius.circular(10))),
              child:  Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text("联系方式",style: TextStyle(color: Colors.black,fontSize: 14.sp),),
                  Container(
                    width: screenSize.width-150.w,
                    height: 35.h,
                    child:  TextField(
                    controller: _phoneNumText,
                      decoration: InputDecoration(
                        hintText: '请输入联系方式',
                        hintStyle:TextStyle(fontSize: 14.sp,color: MyColors.hintColor),
                        filled: true,
                        fillColor: Colors.white,
                        border: InputBorder.none,
                        alignLabelWithHint: true,
                      ),
                      textAlign: TextAlign.right,
                      style: TextStyle(color: Colors.black),
                      onChanged: (value) {
                        setState(() {
                          phoneNum=value;
                        });
                      },
                    ),
                  ),
                ],
              ),
            ),
            GestureDetector(
              child:   Container(
                padding: EdgeInsets.only(top: 10.h,bottom: 10.h,left: 10.w,right: 10.w),
                margin: EdgeInsets.only(left: 10.w,right: 10.w,top: 10.h),
                decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.all(Radius.circular(10))),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [

                    Text(widget.addressType==1?"装货时间":"卸货时间",style: TextStyle(color: Colors.black,fontSize: 14.sp),),
                    Row(
                      children: [
                        startTimeDay!=""&&startTimeHour!=""?
                        Text(startTimeDay+" "+startTimeHour,style: TextStyle(color: Colors.black,fontSize: 14.sp),):
                        Text("请选择时间",style: TextStyle(color: MyColors.hintColor,fontSize: 14.sp),),
                        Icon(Icons.arrow_forward_ios,color: MyColors.authTipColor,size: 20,),
                      ],
                    ),
                  ],
                ),
              ),
              onTap: ()async{
                final result=await showModalBottomSheet(context: context, builder: (BuildContext context) {
                  return ChooseTimeSheet(chooseTimeType: 1, timeDay: startTimeDay, timeHour: startTimeHour, timeCode: startTimeCode,);
                });
                if(result!=null){
                  setState(() {
                    startTimeDay = result[0];
                    startTimeHour = result[1];
                    startTimeCode =result[2];
                  });

                }
              },
            ),


          ],
          ),
          ),
          Container(
            height: 85.h,
            padding: EdgeInsets.only(left: 15.w,right: 15.w,top: 10.h,bottom: 40.h),
            color: Colors.white,
            child: BrnSmallMainButton(
              width: screenSize.width-30.w,
              title: '确定',
              bgColor: MyColors.themeColor,
              textColor: Colors.white,
              fontSize: 16.sp,
              fontWeight: FontWeight.w600,
              radius: 30,
              onTap: () async{
                if(detailAddressOne==""||detailAddressOne.isEmpty){
                BrnToast.show("请填写地址信息", context);
                // }else if(phoneNum!=""&&phoneNum.length!=11){
                //   BrnToast.show("请输入正确的手机号码", context);
                }else {
                  Navigator.pop(context, [
                    unitName,
                    startAddressOne,
                    startCitycodeOne,
                    detailAddressOne,
                    contextPerson,
                    phoneNum,
                    startTimeDay,
                    startTimeCode,
                    startTimeHour,
                    startCitylocation,
                    startCitynameOne
                  ]);
                }
              },
            ),
          ),
        ],
      ),
    );
  }
}


